<?php
/* ----------------------------------------------------------
--               JacoCMS by Jaco Ruit                      --
-------------------------------------------------------------
--     JacoCMS is Open Source and is lincenced under       --
--             GNU General Public Licence v3               --
--          http://www.gnu.org/copyleft/gpl.html           --
-------------------------------------------------------------
--       Please don't remove any text from the footers.    --
-------------------------------------------------------------
-- Credits:   * Jaco Ruit          ~     Scripts,MySQL     --     
-----------------------------------------------------------*/

# JacoCMS Video Class


class Video
{
	public function getByID($idtosearch)
	{
		$keywords = "";
		$query = "SELECT url, title, date, text, author, keywords FROM videos WHERE id='" . $idtosearch . "'";
		$result = mysql_query($query);
		while ($row = mysql_fetch_assoc($result)) 
		{
			$id = $idtosearch;
			$date = $row['date'];
			$title = $row ['title'];
			$text = $row['text'];
			$author = $row['author'];
			$url = $row['url'];
			$keywords = $row['keywords'];
		}
		$arr = array("id" => $id, "date" => $date, "author" => $author, "title" => $title, "text" => $text, "url" => $url, "keywords" => $keywords);
		mysql_free_result($result);
		return $arr;
	}
	
	public function getLastID()
	{
		$query = "SELECT id FROM videos ORDER BY id DESC";
		$result = mysql_query($query);
		$lastid = null;
		$lastid = mysql_fetch_array($result);
		mysql_free_result($result);
		return $lastid['id'];
	}
	
	public function delete($idtodel)
	{
		$query = "DELETE FROM videos WHERE id='" . $idtodel . "'";
		$result = mysql_query($query);
	}
	
	public function add ($date, $author, $title, $url, $text, $keywords)
	{
		$newid = $this->getLastID() + 1;
		$query = "INSERT INTO videos (id,date,author,title,url,text,keywords) VALUES ('" . $newid . "','" . $date . "','" . $author . "','" . $title . "','" . $url . "','" . $text . "','" . $keywords . "')";
		$result = mysql_query($query);
	}
	
	public function update ($idtoup, $title, $url, $text, $keywords)
	{
		$query = "UPDATE videos SET title = '" . $title . "', url = '" . $url . "', text = '" . $text . "', keywords =  '" . $keywords . "' WHERE id='" . $idtoup . "'";
		$result = mysql_query($query);
	}
	
	public function rowCount()
	{
		$query = "SELECT title FROM videos";
		$result = mysql_query($query);
		$count = mysql_num_rows($result);
		return $count;
	}
	
	public function getHTMLCode($array)
	{
		return'<div id="video"></div>
		<script type="text/javascript"> jwplayer("video").setup({ flashplayer: "jwplayer/player.swf", file: "' . $array['url']  . '", height: 450, width: 480 }); </script>
				<br />
				<p>' . $array['text'] . '</p><br />
			
				<!-- AddThis Button BEGIN -->
					<div class="addthis_toolbox addthis_default_style addthis_32x32_style">
						<a class="addthis_button_preferred_1"></a>
						<a class="addthis_button_preferred_2"></a>
						<a class="addthis_button_preferred_3"></a>
						<a class="addthis_button_preferred_4"></a>
						<a class="addthis_button_compact"></a>
						<a class="addthis_counter addthis_bubble_style"></a>
					</div>	<div id="articlefooter">
					<script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#pubid=xa-4e2f390741f02fb9"></script>
					<!-- AddThis Button END --><p>Posted on ' . $array['date'] . ' by ' . $array['author'] . ' </p>
				</div>';
	}
	
	public function getShortHTMLCode($array)
	{
		return '<div id="article">
					<h3>' . $array['title'] . '</h3>
					<br />
					<div id="center"><p><b>' . $array['text'] . '</b></p></div>
					<br />
			<div id="articlefooter">
			<p>Posted on ' . $array['date'] . ' by ' . $array['author'] . ' ~ <a href="video-' . $array['id'] . '">Watch</a></p>
			</div></div>';
	}
	
	public function getHTMLGalleryCode($from, $till)
	{
		$htmlcode = "";
		if ($till > $this->getLastID())
		{
			return '<div id="error"><p>Decrease search!</p></div>';
		}
		if ($from > $this->getLastID())
		{
			return '<div id="error"><p>Decrease search!</p></div>';
		}
		$query = "SELECT id, url, title, date, text, author FROM videos ORDER BY id DESC LIMIT " . $from .", " . $till;
		$result = mysql_query($query);
		while ($row = mysql_fetch_assoc($result)) 
		{
			$id = $row['id'];
			$date = $row['date'];
			$title = $row ['title'];
			$text = $row['text'];
			$author = $row['author'];
			$url = $row['url'];
			
			$arr = array("id" => $id, "date" => $date, "author" => $author, "title" => $title, "text" => $text, "url" => $url);
			$htmlcode .= $this->getShortHTMLCode($arr);
		}
		
		mysql_free_result($result);
		return $htmlcode;
	}
	
	public function getMetaTags($array)
	{
		return '<meta name="author" content="' . $array['author'] . '" />
				<meta name="description" content="' . $array['text'] . '" />
				<meta name="keywords" content="' . $array['keywords'] . '" />
				<meta name="generator" content="JacoCMS" />';
	}
	
		public function getTable($array)
	{
		return '<tr><td>' . $array['id'] . '</td><td>' . $array['author'] . '</td><td>' . $array['url'] . '</td><td><a href="deletev-' . $array['id'] . '">Delete</a> <a href="editvideo-' . $array['id'] . '">Edit</a></td></tr>';
	}
	
	
	
	public function getWholeTable($from, $till)
	{
		$htmlcode = "<table><tr><th>ID</th><th>Author</th><th>Video URL</th></tr>";
		if ($till > $this->getLastID())
		{
			return '<div id="error"><p>Decrease search!</p></div>';
		}
		if ($from > $this->getLastID())
		{
			return '<div id="error"><p>Decrease search!</p></div>';
		}
		$query = "SELECT url, author, id FROM videos ORDER BY id DESC LIMIT " . $from .", " . $till;
		$result = mysql_query($query);
		while ($row = mysql_fetch_assoc($result)) 
		{
			$id = $row['id'];
			$url = $row ['url'];
			$writer = $row['author'];
			$arr = array("id" => $id, "author" => $writer, "url" => $url);
			$htmlcode .= $this->getTable($arr);
		}
		$htmlcode .= '</table>';
		mysql_free_result($result);
		return $htmlcode;
	}
	
	public function checkIfExists($idtosearch)
	{
		$query = "SELECT title FROM videos WHERE id='" . $idtosearch . "'";
		$result = mysql_query($query);
		if (mysql_num_rows($result) == 1)
		{
			mysql_free_result($result);
			return true;
		}
		else
		{
			mysql_free_result($result);
			return false;
		}
	}
}
?>